/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package coba.service.impl;

import coba.service.base.ConcurrenciaRecorridoLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;

import coba.model.ConcurrenciaRecorrido;
import coba.model.SolicitudItinerario;
import coba.model.ItinerarioRecorrido;
import coba.model.Recorrido;
import coba.service.base.SolicitudItinerarioLocalServiceBaseImpl;

import java.util.Date;
import java.util.List;
import java.util.LinkedList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.HashSet;

import coba.NoSuchRecorridoException;


/**
 * The implementation of the ConcurrenciaRecorrido local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link coba.service.ConcurrenciaRecorridoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author rodri042
 * @see coba.service.base.ConcurrenciaRecorridoLocalServiceBaseImpl
 * @see coba.service.ConcurrenciaRecorridoLocalServiceUtil
 */
public class ConcurrenciaRecorridoLocalServiceImpl
	extends ConcurrenciaRecorridoLocalServiceBaseImpl {

	public void updateItinerario(long itinerarioId, Date fecha, long cantidadPersonas)
			throws SystemException {

		for (ItinerarioRecorrido itinerarioRecorrido : itinerarioRecorridoPersistence.findByItinerarioId(itinerarioId)) {
			update(itinerarioRecorrido.getRecorridoId(),
					fecha, itinerarioRecorrido.getHora(), cantidadPersonas);
		}

	}
	
	public ConcurrenciaRecorrido update(long recorridoId, Date fecha, long hora, long cantidadPersonas)
			throws SystemException {

		List<ConcurrenciaRecorrido> listConcurrenciaRecorrido = concurrenciaRecorridoPersistence.
				findByRecorridoAndHoraAndFecha(recorridoId, hora, fecha);

		if (listConcurrenciaRecorrido.isEmpty()) {
			ConcurrenciaRecorrido concurrenciaRecorrido = concurrenciaRecorridoPersistence.
					create(counterLocalService.increment(ConcurrenciaRecorrido.class.getName()));

			concurrenciaRecorrido.setRecorridoId(recorridoId);
			concurrenciaRecorrido.setFecha(fecha);
			concurrenciaRecorrido.setHora(hora);
			concurrenciaRecorrido.setCantidadPersonas(cantidadPersonas);

			return super.addConcurrenciaRecorrido(concurrenciaRecorrido);
		}
		else {
			ConcurrenciaRecorrido concurrenciaRecorrido =
					listConcurrenciaRecorrido.get(0);
			
			concurrenciaRecorrido.setCantidadPersonas(
					concurrenciaRecorrido.getCantidadPersonas() + cantidadPersonas);

			return super.updateConcurrenciaRecorrido(concurrenciaRecorrido);
		}

	}

	public Set<Date> findFechasNoPosibles(long recorridoId, long hora, Date fechaDesde, long cantidadPersonas)
			throws SystemException, NoSuchRecorridoException {

		Recorrido recorrido = recorridoPersistence.findByPrimaryKey(recorridoId);

		Set<Date> setDate = new HashSet<Date>();

		List<ConcurrenciaRecorrido> listConcurrenciaRecorrido = this.
				findByRecorridoAndHoraAndFechaDesde(recorridoId, hora, fechaDesde);

		for (ConcurrenciaRecorrido concurrenciaRecorrido : listConcurrenciaRecorrido) {
			if (concurrenciaRecorrido.getCantidadPersonas() + cantidadPersonas > recorrido.getCapacidad())
				setDate.add(concurrenciaRecorrido.getFecha());
		}

		return setDate;

	}

	public long concurreciaByRecorridoAndHoraAndFecha(long recorridoId, long hora, Date fecha) throws SystemException {
		List<ConcurrenciaRecorrido> listConcurrenciaRecorrido = concurrenciaRecorridoPersistence.findByRecorridoAndHoraAndFecha(recorridoId, hora, fecha);
		return !listConcurrenciaRecorrido.isEmpty() ? listConcurrenciaRecorrido.get(0).getCantidadPersonas() : 0;
	}

	public List<ConcurrenciaRecorrido> findByRecorridoAndHoraAndFechaDesde(long recorridoId, long hora, Date fechaDesde)
			throws SystemException {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(ConcurrenciaRecorrido.class);

		query.add(PropertyFactoryUtil.forName("recorridoId")
				.eq(recorridoId));

		query.add(PropertyFactoryUtil.forName("hora")
				.eq(hora));

		query.add(PropertyFactoryUtil.forName("fecha")
				.gt(fechaDesde));

		return concurrenciaRecorridoPersistence.findWithDynamicQuery(query);

	}

}
